<div class="content">
	<div class="tab_bar">
		<ul>
			<li class="tab_index" onclick="setTabContent(this);">用户组赋权</li>
		</ul>
		<button class="btn_tabbar" onclick="submitActionForm(this);" return="group/list.do">保 存</button>
	</div>
	<form id="myForm"  action="group/granting.do" method="post">
 	<div class="tab_content">
		<div class="tab_item tab_show"> 
		    <input name="gid" value="${gid?c}" type="hidden" />
			<table class="table_for_list">
				<thead>
				<tr>
					<th width="30" class="quanxuan" onclick="allCheck();">全选</th> 
			        <th>菜单名称</th>
			        <th>URL地址</th>
			        <th>排序</th>
			        <th>描述</th>
			      </tr>
				</thead>
			    <tbody>
				  <@menuTree id = 0 node = -1/>
				</tbody>
			</table>
		</div>
	</div>
	</form>
</div>
<script type="text/javascript">


var auths = '<#list permissions as per>${per.mid},</#list>';// 逗号隔开
var arrays = new Array();
arrays = auths.split(",");
for(var i=0; i < arrays.length; i++){
	$("input[value="+trim(arrays[i])+"]").attr("checked","true");
}


/**
$("input[type='checkbox']").change(function(){
	var tr = $(this).parent().parent();  
	if($(this).attr("checked") == 'checked'){
		checkedParentMenu(tr,true ); // 选择父菜单
	}else{
		checkedParentMenu(tr,false ); // 选择父菜单
	}
});
*/
/**
 * 让父菜单被选择
 @param obj table 中的tr节点
 */
function checkedParentMenu(tr, status){
	var cid = tr .attr("cid") - 1; // 当前tr的父ID
	 var ttr = tr.prev();
	 var previd = ttr.attr("cid");
	 if( cid ==previd ){// 找到了父菜单
	 	$(ttr.find("input[type='checkbox']")[0]).attr("checked",status);
	     if(previd != 0){ // 如果没有到根节点
	    	 checkedParentMenu(ttr,status);
	     }
	 }else{
		 checkedParentMenu(ttr,status);
	 }
}



/* **********************
* 删除一条或者多条数据(通用方法)
* 例子:  <button onClick="deleteCheck(this);" id="staff_delete.action" id="stafffind.action">删除</button>
* id  -deleteAction地址
* callback  -删除成功回调页面地址
* ********************* */
function submitForm(obj){ 
	submitActionForm(obj); 
}

//去空格函数
function trim(str){   
    return str.replace(/(^\s*)|(\s*$)/g, '');   
}

</script>
 

<!#-- 独立的菜单树生成 --#>
<#macro menuTree id node>
	<#local temp = node + 1 />
	<#list menus as menu>
		<#if menu.id != 0>
		  	<#if id = menu.pid>
				<tr cid="${temp?c}" >
					<td class="checkbox1"><input name="mid" type="checkbox" value="${menu.id?c}" /></td>
			        <td>
			        	<#if temp gt 0 ><#list 1..temp as x>&nbsp; &nbsp;</#list>├</#if> <i class="fa ${menu.icon!}"></i> ${menu.name!} 
			        </td>
			        <td>${menu.url!} </td>
			        <td class="tac">${menu.sort?c} </td>
			        <td>${menu.description!} </td> 
			     </tr>
		    <@menuTree id = menu.id node=temp/>
	      	</#if>
        </#if>
	</#list> 
</#macro>

